package cn.modoumama.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/** 
 * 类描述：TODO(用一句话描述该类做什么)<br>
 * <br/>
 * 创建人：邓强   <br>
 * 创建时间：2017年11月27日 上午11:09:10    <br> 
 * 修改人：  <br>
 * 修改时间：2017年11月27日 上午11:09:10   <br>  
 * 修改备注：     <br>
 * @version   V1.0      
 */
public class HttpToHttpsFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest requestt, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)requestt;
		StringBuffer host = request.getRequestURL();
		
		if(host.substring(4,5).equals("s")){
			chain.doFilter(requestt, response);
		}else{
			String qstr = request.getQueryString();
			host.insert(4, 's');
			if (qstr != null) {
				host.append("?" + qstr);
			}
			((HttpServletResponse)response).sendRedirect(host.toString());
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}
}
